<template>

	<view class="t-login">
		<image class="loginBg" src="@/static/images/index/loginBg.png" mode="widthFix"></image>
		<view class="t-b">{{ globalConfig.appInfo.name }}</view>
		<view class="tabs">
			<!-- <view :class="currentTab === 0?'light':'dark'" @click="changeTab(0)">账号登录</view> -->
			<!-- <view class="line"></view>
			<view :class="currentTab===1?'light':'dark'" @click="changeTab(1)">验证码登录</view> -->
		</view>

		<form style="display: 100rpx;" v-if="currentTab==0">
			<view class="cu-form-group input-item">
				<!-- <view class="title">账号</view> -->
				<!-- <uni-icons type="person" size="30" color="#0081ff"></uni-icons> -->
				<text class="cuIcon-people text-blue"></text>
				<u-input type="text" class="margin-left-lg"  border='none' v-model="loginForm.username" style="font-size: 35rpx;"
					placeholder="请输入账号" />

			</view>
			<view class="cu-form-group input-item">
				<!-- <view class="title">密码</view> -->
				<!-- <uni-icons type="locked" size="30" color="#0081ff"></uni-icons> -->
				<text class="cuIcon-lock text-blue"></text>
				<u-input type="text" class="margin-left-lg"  border='none' v-model="loginForm.password" style="font-size: 35rpx;"
					placeholder="请输入密码" password />
			</view>
			<view class="remberBox">
				  <u-checkbox-group v-model="checkboxValue1" placement="column"  @change="checkboxChange">
						<u-checkbox v-for="(item, index) in checkboxList1" :key="index" :label="item.name"
							:name="item.name"
							size='36' labelSize='36' shape="circle"
						>
						</u-checkbox>
					</u-checkbox-group>
				
				
			</view>
			<view class="flex flex-direction input-item">
				<button class="cu-btn btnBtm" @click="handleLogin">
					<text class="text-xl">立即登录</text>
				</button>
				<!-- <button class="cu-btn bg-gradual-green " @click="goRegister">
					<text class="text-xl">去 注 册</text>
				</button> -->
			</view>
			<view class="touristTitle" @click="touristLogin"  v-if="!globalConfig.isToB">
				暂不登录
			</view>
		</form>
		<form style="display: 100rpx;" v-if="currentTab==1">
			<view class="cu-form-group input-item">
				<text class="cuIcon-people text-blue"></text>
				<u-input type="text" class="margin-left-lg" border='none' v-model="loginFormCode.phone" 
					placeholder="请输入手机号">
					<template slot="suffix">
						<u-code ref="uCode" @change="codeChange" seconds="20" changeText="X秒重新获取"></u-code>
						<u-button @tap="getCode" :text="tips" type="success" size="mini"></u-button>
					</template>
				</u-input>

			</view>
			<view class="cu-form-group input-item">
				<text class="cuIcon-lock text-blue"></text>
				<u-input type="text" class="margin-left-lg" border='none'  v-model="loginFormCode.code" 
					placeholder="请输入验证码" />

			</view>
			
			<view class="flex flex-direction input-item" >
				<button class="cu-btn btnBtm" @click="handleLoginCode">
					<text class="text-xl">立即登录</text>
				</button>
			</view>
			<view class="touristTitle" @click="touristLogin">
				暂不登录
			</view>
		</form>
	</view>
</template>

<script>
	import config from '@/config'

	export default {
		data() {
			return {
				// subTitle: getApp().globalData.config.appInfo.name,
				// globalConfig: getApp().globalData.config,
				globalConfig: config,
				loginForm: {
					username: "",
					password: "",
				},
				loginFormCode:{
					phone:"",
					code:"",
				},
				currentTab: 0,
				tips: '',
				checkboxValue1:[],
				checkboxList1: [{
					name: '记住密码',
					disabled: false
				}],
				
			}
		},
		mounted() {
			if(uni.getStorageSync('loginForm')){
				this.loginForm=uni.getStorageSync('loginForm')
			}
			
		},

		methods: {
			checkboxChange(n) {
				this.checkboxValue1=n
			},
			touristLogin() {
				this.$tab.reLaunch('/pages/index/index')

			},
			changeTab(index) {
				this.currentTab = index;
				if (this.currentTab == 2) {
					this.showAccredit = true
				}
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if(this.loginFormCode.phone){
					if(uni.$u.test.mobile(this.loginFormCode.phone)){
						if (this.$refs.uCode.canGetCode) {
							// 模拟向后端请求验证码
							uni.showLoading({
								title: '正在获取验证码'
							})
							setTimeout(() => {
								uni.hideLoading();
								// 这里此提示会被this.start()方法中的提示覆盖
								uni.$u.toast('验证码已发送');
								// 通知验证码组件内部开始倒计时
								this.$refs.uCode.start();
							}, 2000);
						} else {
							uni.$u.toast('倒计时结束后再发送');
						}
					}else{
						this.$modal.showToast('请检查输入的手机号~')
					}
					
				}else{
					this.$modal.showToast('请输入手机号~')
				}
				
			},

			getWeiLogin() {
				if (this.$store.state.user.token == undefined || this.$store.state.user.token == null || this.$store.state
					.user.token == '') {
					uni.removeStorageSync('openId')
					this.$wxH5Login.wxLogin() //微信授权
				}
			},
			goRegister() {
				uni.redirectTo({
					url: "/pages/register/register"
				})

			},
			handleLoginCode(){
				if (this.loginFormCode.phone === "") {
					this.$modal.showToast("请输入您的手机号~")
				} else if (this.loginFormCode.code === "") {
					this.$modal.showToast("请输入您的验证码~")
				} else {
					this.$modal.showToast("正在开发~")
				}
				
				
			},
			// 登录方法
			async handleLogin() {
				if (this.loginForm.username === "") {
					this.$modal.showToast("请输入您的账号")
				} else if (this.loginForm.password === "") {
					this.$modal.showToast("请输入您的密码")
				} else {
					// this.$modal.loading("登录中，请耐心等待...")
					this.pwdLogin()
				}
			},

			// 密码登录
			async pwdLogin() {
				this.$store.dispatch('Login', this.loginForm).then(() => {
					this.$modal.closeLoading()
					// this.loginSuccess()
					if(this.checkboxValue1.length>0){
						uni.setStorageSync('loginForm',this.loginForm)
					}else{
						uni.removeStorageSync('loginForm')
					}
					this.$tab.reLaunch('/pages/index/index')
				}).catch((error) => {
					this.$modal.closeLoading()
				})
			},
			// 登录成功后，处理函数
			loginSuccess(result) {
				// 设置用户信息
				// this.$tab.reLaunch('/pages/index/index')
				this.$store.dispatch('GetInfo').then(res => {
					this.$tab.reLaunch('/pages/index/index')
				})
			},

		}
	}
</script>

<style lang="scss" scoped>
	.input-item {
		margin-top: 40rpx;
		border-radius: 30px;
	}
	.remberBox{
		display: flex;
		justify-content: center;
		align-items: center;
		height: 40rpx;
		margin-top: 40rpx;
		
	}

	.t-login {
		// width: 650rpx;
		padding: 50rpx;
		height: 100vh;
		margin: 0 auto;
		padding-top: 10%;
		font-size: 28rpx;
		color: #000;
		background-color: #F0F5FF;
		
		button {
			font-size: 28rpx;
			background: #5677fc;
			color: #fff;
			height: 90rpx;
			line-height: 90rpx;
			border-radius: 50rpx;
			box-shadow: 0 5px 7px 0 rgba(86, 119, 252, 0.2);
		}
		.t-b {
			text-align: center;
			font-size: 45rpx;
			color: #282C35;
			padding: 240rpx 0 20rpx 0;
			font-weight: bold;
		}
		.loginBg{
			position: absolute;
			width: 304rpx;
			height: 380rpx;
			top: 0;
			right: 0;
		}
		.btnBtm {
			// margin-bottom: 40rpx;
			background-image: linear-gradient(45deg, #2676E8, #58C2FB) !important;
		}
		
		.tabs {
			display: flex;
			justify-content: space-evenly;
			align-items: center;
			font-size: 32rpx;
			padding: 44rpx 0 20rpx;
		
			.line {
				width: 12rpx;
				height: 42rpx;
				border-radius: 6rpx;
				background-color: #0993E7;
			}
		
			.dark {
				color: #9F9F9F;
			}
		
			.light {
				color: #0993E7;
			}
		}
		.touristTitle {
			text-align: center;
			color: #0993E7;
			margin-top: 33rpx;
		}
	}

</style>